<!-- 详情页布局容器 -->
<template>
    <div class='xtx-goods-page' v-if="goods">
        <div class="container">
            <!-- 面包屑 -->
            <XtxBread>
                <XtxBreadItem to="/">首页</XtxBreadItem>
                <XtxBreadItem :to="'/category/' + goods.categories[1].id">{{ goods.categories[1].name }}
                </XtxBreadItem>
                <XtxBreadItem :to="'/category/sub/' + goods.categories[0].id">{{ goods.categories[0].name }}
                </XtxBreadItem>
                <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
            </XtxBread>
            <!-- 商品信息 -->
            <div class="goods-info">
                <!-- 左边图片区 -->
                <div class="media">
                    <GoodsImage :images="goods.mainPictures"></GoodsImage>
                    <GoodsSales></GoodsSales>
                </div>
                <!-- 右边信息区 -->
                <div class="spec">
                    <!-- 商品基本信息展示 -->
                    <GoodsName :goods="goods"></GoodsName>
                    <!-- 规格组件：sku款式选择 -->
                    <GoodsSku :goods="goods" @change="changeSku"></GoodsSku>
                    <!-- 数量组件 -->
                    <XtxNumbox label="数量" v-model="num" :max="goods.inventory" />
                    <!-- 加入购物车组件 -->
                    <XtxButton @click="addToCarBtn" type="primary" style="margin-top:20px;">加入购物车</XtxButton>
                </div>
            </div>
            <!-- 同类商品推荐 -->
            <GoodsRelevant :goodsId="goods.id" />
            <!-- 商品详情 -->
            <div class="goods-footer">
                <div class="goods-article">
                    <!-- 商品+评价 -->
                    <GoodsTabs :goods="goods"></GoodsTabs>
                    <!-- 注意事项 -->
                    <GoodsWarn />
                </div>
                <!-- 24热榜+专题推荐 -->
                <div class="goods-aside">
                    <GoodsHot :goodsId="goods.id" :type="1"></GoodsHot>
                    <GoodsHot :goodsId="goods.id" :type="2"></GoodsHot>
                    <GoodsHot :goodsId="goods.id" :type="3"></GoodsHot>
                </div>
            </div>
        </div>
    </div>
</template>
  
<script>
import { findGoods } from '@/api/product'
import { ref, watch, nextTick, provide } from 'vue'
import GoodsRelevant from './components/goods-relevant'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
export default {
    name: 'GoodsPage',
    components: { GoodsWarn, GoodsHot, GoodsTabs, GoodsSku, GoodsRelevant, GoodsSales, GoodsName, GoodsImage },
    setup() {
        const { goods } = useGoods()
        // sku改变时候触发
        const changeSku = (sku) => {
            if (sku.skuId) {
                goods.value.price = sku.price
                goods.value.oldPrice = sku.oldPrice
                goods.value.inventory = sku.inventory
                goods.value.specsText = sku.specsText
                goods.value.currSkuId = sku.skuId
            } else {
                goods.value.currSkuId = ''
            }
        }
        // 数量
        const num = ref(1)
        const store = useStore()
        const addToCarBtn = () => {
            // 判断是否选择了完整规格
            if (!goods.value.currSkuId) return Message({ type: 'warn', text: '请选择完整规格' })
            // 库存不足提示
            if (num.value > goods.value.inventory) return Message({ type: 'warn', text: '库存不足了' })
            // 约定本地存储的信息内容 - 字段名可参考购物车列表接口的字段名
            const cartItem = {
                id: goods.value.id,
                name: goods.value.name,
                picture: goods.value.mainPictures[0],
                price: goods.value.price,
                skuId: goods.value.currSkuId,
                attrsText: goods.value.specsText,
                nowPrice: goods.value.price,
                stock: goods.value.inventory,
                count: num.value,
                selected: true,
                isCollect: false,
                isEffective: true,
                discount: 1
            }
            // 添加到 VueX 仓库中，VueX 数据做了本地存储同步，所以可以通过本地存储查看数据
            store.dispatch('cart/insertCart', cartItem).then(msg => {
                Message({ type: 'success', text: '加入购物车成功' })
            })
        }

        provide('goods', goods)
        return { goods, changeSku, num, addToCarBtn }
    }
}
// 获取商品详情
const useGoods = () => {
    const goods = ref(null)
    const route = useRoute()
    watch(() => route.params.id, (newVal) => {
        if (newVal && `/product/${newVal}` === route.path) {
            findGoods(route.params.id).then(data => {
                goods.value = null
                //nextTick有延时效果 让商品数据为null让后使用v-if的组件可以重新销毁和创建
                nextTick(() => {
                    goods.value = data.result
                })
            })
        }
    }, { immediate: true })
    return { goods }
}
</script>
  
<style scoped lang='less'>
.goods-info {
    min-height: 600px;
    background: #fff;
    display: flex;

    .media {
        width: 580px;
        height: 600px;
        padding: 30px 50px;
    }

    .spec {
        flex: 1;
        padding: 30px 30px 30px 0;
    }
}

.goods-footer {
    display: flex;
    margin-top: 20px;

    .goods-article {
        width: 940px;
        margin-right: 20px;
    }

    .goods-aside {
        width: 280px;
        min-height: 1000px;
    }
}
</style>